//路由器
app.config(function($routeProvider){
    $routeProvider.
    when("/",{
       controller: 'notice',
       templateUrl: 'view/notice.html',
        resolve : {
          async : getJSON(urlAddress + "/home/account/json/notice.json")
       }
    }).
    when("/index",{
       //首页，默认消息通知页面
       controller: 'notice',
       templateUrl: 'view/notice.html',
        resolve : {
          async : getJSON(urlAddress + "/home/account/json/notice.json")
       }
    }).
    when("/notice",{
       //消息通知页面
       controller: 'notice',
       templateUrl: 'view/notice.html',
        resolve : {
          async : getJSON(urlAddress + "/home/account/json/notice.json")
       }
    }).
    when("/account",{
       //账号管理页面
       controller: 'account',
       templateUrl: 'view/account.html',
       resolve : {
          async : getJSON(urlAddress + "/home/account/json/account.json")
      }
    }).
    when("/accounter",{
       //账号管理页面
       controller: 'accounter',
       templateUrl: 'view/accounter.html',
       resolve : {
          async : getJSON(urlAddress + "/home/account/json/accounter.json")
      }
    }).
    otherwise({
       redirectTo:'/'
    });
});

//消息通知的控制器
app.controller('notice',["$scope","async",function($scope,msg) {
    //用户总数
    $scope.number = msg.init.userNumber;
    //当前的页数
    $scope.nowPage = 1;
    // 最大的页数
    $scope.maxPage = 8;

    $scope.hasLastPage = true;
    $scope.pageN = $scope.nowPage;

    // 分页功能
    $scope.pageA = getPageArr($scope.nowPage,$scope.maxPage);
    $scope.afterLi = $scope.pageA.afterLi;
    $scope.beforeLi = $scope.pageA.beforeLi;
    $scope.pageArr = $scope.pageA.arr;
    $scope.hasLastPage = $scope.pageA.hasLastPage;
    $("pages li").each(function() {
      var n = parseInt($(this).html());
    })

    //将获取的数据跟传给控制器
    $scope.opints = msg.data;
    if ($scope.opints.length < 1) {
     $scope.hasSign = true;
    } else {
     $scope.hasSign = false;
    }

    //控制行数
    $scope.lineNumbers = [10,15,20];
    $scope.lineNumber = $scope.lineNumbers[0];

    //删除信息
    $scope.delUser = function() {
    // 获取所有被选中的元素及其对应的用户ID
    var delID = [];
    $("table td input").each(function() {
       if ($(this).prop("checked")) {
          delID.push($(this).parents("tr").attr("userID"));
       };
    })
     if(delID.length == 0) {
       alert("你没有选择任何用户");
     } else {
      console.log(delID);
        // window.location.reload();
     }
    }
    // 增加消息及相应的表单验证(大部分验证在HTML当中)
    $scope.addForm = true;
    $scope.addUser = function(bool) {
    $scope.addForm = bool;
    }
    $scope.checkedUs = function() {
      console.log($scope.checkUs);
    }
    $scope.devBool = function(bool) {
    $scope.userform.devBlur = bool;
    }
    $scope.addressBool = function(bool) {
    $scope.userform.addressBlur = bool;
    }
    $scope.accBool = function(bool) {
    $scope.userform.accBlur = bool;
    }

    // 编辑表单
    $scope.write = function(x) {
    $scope.addForm = true;
    $scope.nt_str = x.notice;
    $scope.nt_title = x.title;
    }
    // 全选效果以及反选
    allChecked();

    //设置导航栏中当前页面的背景色
    navTitClass($("nav .notice").parent().eq(0));
}])
       
//管理员权限的控制器
app.controller('account',["$scope","async",function($scope,msg) {
     //设备总数
     $scope.number = msg.init.userNumber;
     //当前的页数
     $scope.nowPage = 1;
     // 最大的页数
     $scope.maxPage = 35;

     $scope.hasLastPage = true;
     $scope.pageN = $scope.nowPage;

      // 分页功能
     $scope.pageA = getPageArr($scope.nowPage,$scope.maxPage);
     $scope.afterLi = $scope.pageA.afterLi;
     $scope.beforeLi = $scope.pageA.beforeLi;
     $scope.pageArr = $scope.pageA.arr;
     $scope.hasLastPage = $scope.pageA.hasLastPage;
     $("pages li").each(function() {
          var n = parseInt($(this).html());
     })

     // 设置初始的状态筛选条件和行数显示
     $scope.lineNumbers = [10,15,20];
     $scope.lineNumber = $scope.lineNumbers[0];

     //将获取的数据跟传给控制器
     $scope.opints = msg.data;
     if ($scope.opints.length < 1) {
         $scope.hasSign = true;
     } else {
         $scope.hasSign = false;
     }

     //删除用户
     $scope.delUser = function() {
          // 获取所有被选中的元素及其对应的用户ID
          var delID = [];
          $("table td input").each(function() {
             if ($(this).prop("checked")) {
                delID.push($(this).parents("tr").attr("userID"));
             };
          })
           if(delID.length == 0) {
             alert("你没有选择任何管理员");
           } else {
              console.log(delID);
              // window.location.reload();
           }
      }

      // 全选效果以及反选
      allChecked();

      //设置导航栏中当前页面的背景色
      navTitClass($("nav .account").parent().eq(0));
}])

// 管理员个人管理中心
app.controller('accounter',["$scope","async",function($scope,async) {
          $scope.urlAddress = urlAddress;
          //初始化数据
          $scope.init = async.init;
          $scope.initFn = function(data) {
              $scope.name = data.name;
              $scope.parts = data.parts;
              $scope.work = data.work;
              $scope.picSrc = urlAddress + "/" + data.picSrc;
          }

          $scope.email = async.init.email;
          $scope.power = async.init.power;
          $scope.initFn($scope.init);

          //控制是否修改信息
          $scope.changeform = true;
          $scope.save = function() {
              $scope.changeform = true;
             $(".sex").addClass("disabled");
          }
          $scope.exit = function() {
                $scope.changeform = true;
               $(".sex").addClass("disabled");
               $scope.initFn(async.init);
          }
          $scope.write = function() {
              $scope.changeform = false;
              $(".sex").removeClass("disabled");
          }

          // 表单验证显示函数 
          $scope.nameblur = function(bool) {
             $scope.nameblur1 = bool;
          }
          $scope.workblur = function(bool) {
             $scope.workblur1 = bool;
          }

          $scope.picFormShow = true;
          $scope.picShow = function(bool) {
              $scope.picFormShow = bool;
          }
          
          $scope.tapconShow = function($event,bool) {
              var tg = $event.target;
              $(tg).removeClass("unshow").siblings().addClass("unshow");
              $scope.tapconshow = bool;
          }

          $scope.checkImg = function($event) {
              var tg = $event.target;
              $("#showPic").attr("src",$(tg).attr("src"));
          }
}]);
